###### Session Information ######
'''
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3
'''
####################################



#install.packages('lme4')
#install.packages("glmmTMB")
#install.packages("MuMIn")
#install.packages("devtools")
library("devtools") #version 2.4.5
devtools::install_local(".././lmSupport_2.9.13.tar.gz")
#install.packages("Matrix", dependencies = TRUE)
library("Matrix") #version 1.6.3
library("tidyr") #version 1.3.0
library("ggplot2") #version 3.4.3
library("dplyr") #version 1.1.4
library("lmSupport") #version 2.9.13
library("ggpubr") #version 0.6.0
library('readxl') #version 1.4.3
library("rstatix") #version 0.7.2
library("lme4") #version  1.1.35.1
library("glmmTMB") #version 1.1.8
library("MuMIn") #version 1.47.5


#import first sheet
dnew = read_excel(".././moral scored_final_dataset_march_22_2023.xlsx")
dnew

# cluster unique channel ids
dnew$channel_id <- dnew %>% dplyr::group_indices(channel_id) # Of observations: 13,745
dnew$channel_id



########################
##### Hypothesis 1 #####
########################

######## Regression_base  ########
######## DV 1: View count ########
### DV is engagement (View count), ### 
### IVs are: {binary: whether she is used in a video or not}, ### 
### {binary: whether he is used in a video or not}, ### 
### CVs are: {channel_id} + {platform: YouTube or TikTok} + {issue: Climate or Vaccine}.(N=13,745) ### 

summary(m1<- glmmTMB(video_view_count ~ female + male + channel_id + platform + issue + (1|channel_id), family = "nbinom2"(link="log"), control = glmmTMBControl(parallel = 5), REML = FALSE, data = dnew))
r.squaredGLMM(m1)


######## Regression_base  ########
######## DV 2: Like count ########
### DV is engagement (Like count), ### 
### IVs are: {binary: whether she is used in a video or not}, ### 
### {binary: whether he is used in a video or not}, ### 
### CVs are: {channel_id} + {platform: YouTube or TikTok} + {issue: Climate or Vaccine}.(N=13,745) ### 

summary(m2<- glmmTMB(video_like_count ~ female + male + channel_id + platform + issue + (1|channel_id), family = "nbinom2"(link="log"), control = glmmTMBControl(parallel = 5), REML = FALSE, data = dnew))
r.squaredGLMM(m2)



######## Regression_moral  ########
######## DV 1: View count ########
### DV is engagement (View count), ### 
### IVs are: {binary: whether she is used in a video or not}, ### 
### {binary: whether he is used in a video or not}, ### 
### CVs are: {Strength scores for each of the six moral foundations} + {channel_id} + {platform: YouTube or TikTok} + {issue: Climate or Vaccine}.(N=13,745) ### 

summary(m3<- glmmTMB(video_view_count ~ female + male + care_strength + fairness_strength + loyalty_strength + authority_strength + sanctity_strength + liberty_strength + channel_id + platform + issue + (1|channel_id), family = "nbinom2"(link="log"), control = glmmTMBControl(parallel = 5), REML = FALSE, data = dnew))
r.squaredGLMM(m3)

######## Regression_moral  ########
######## DV 2: Like count ########
### DV is engagement (Like count), ### 
### IVs are: {binary: whether she is used in a video or not}, ### 
### {binary: whether he is used in a video or not}, ### 
### CVs are: {Strength scores for each of the six moral foundations} + {channel_id} + {platform: YouTube or TikTok} + {issue: Climate or Vaccine}.(N=13,745) ### 

summary(m4<- glmmTMB(video_like_count ~ female + male + care_strength + fairness_strength + loyalty_strength + authority_strength + sanctity_strength + liberty_strength + channel_id + platform + issue + (1|channel_id), family = "nbinom2"(link="log"), control = glmmTMBControl(parallel = 5), REML = FALSE, data = dnew))
r.squaredGLMM(m4)


